home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / math / gle-3.000 / gle-3 / gle / dvipj.c < prev    next >
C/C++ Source or Header  |  1995-02-07  |  7KB  |  299 lines

  1. /* data compression removed from sendline bit as some hp printers
  2.    don't  understand the compressed data format,  -plus will re-enable it */
  3.  
  4. #define XSIZECM 20.32        /* at 120 DPI  */
  5. #define YSIZECM 25.4        /* at 72 DPI */
  6. #define NXBITS 1440 /* 1064     8 inches */
  7. #define NYBITS 1800  /* 1424     10 inches */
  8. extern int hp_plus;
  9. #ifdef __TURBOC__
  10. #include <alloc.h>
  11. #endif
  12. #include "all.h"
  13. #include <math.h>
  14. #include "core.h"
  15. #include "mygraph.h"
  16. #include "mydev.h"
  17. #define dp if (dop)
  18. #ifndef __TURBOC__
  19. #define huge
  20. #endif
  21. #define true (!false)
  22. #define false 0
  23. int dop=false;
  24.  
  25. int d_open(double x, double y);
  26. extern int dev_low;
  27. extern int cur_colori, cur_filli;
  28. extern int gdebug;
  29. extern int nxbits,nybits;
  30. #define dbg if (gdebug==true)
  31. int bitmap_print(void);
  32. int bitmap_free(void);
  33. int bitmap_alloc(void);
  34. int bitmap_paint(int row, int x1, int x2);
  35. int printmem(char *x,int n);
  36. void pprintf(va_list arg_list, ...);
  37. int dvitype(void);
  38.  
  39. /*--------------------------------------------------------------*/
  40. /*    Bitmap output for EPSON printers             */
  41. /*--------------------------------------------------------------*/
  42. int bitmap_print(void);
  43. char *bitmap_line(int y);
  44. int bitmap_size(int *xbits, int *ybits, double *width, double *height);
  45. bitmap_size(int *xbits, int *ybits, double *width, double *height)
  46. {
  47.     *xbits = NXBITS; *ybits = NYBITS; *width = XSIZECM; *height = YSIZECM;
  48.     if (dev_low) {*xbits = *xbits/2; *ybits = *ybits/2;}
  49.  
  50. }
  51. int print_row(int i);
  52. #ifndef __TURBOC__
  53. uint32 coreleft(void)
  54. {
  55.     return 4000000;
  56. }
  57. #endif
  58.  
  59. bitmap_free(void)
  60. {
  61.     printf("core left %ld \n",coreleft());
  62. }
  63. bitmap_alloc(void)
  64. {
  65. }
  66. unsigned int grey_bits[] = {0x0000, 0x0200, 0x0802, 0x0a02,
  67.         0x5050, 0x5250, 0x5852, 0x5a52,
  68.         0xa5a5, 0xa7a5, 0xada7, 0xafa7,
  69.         0xf5f5, 0xf7f5, 0xfdf7, 0xfff7, 0xffff};
  70. unsigned int cur_bits[4];
  71. #define NGREY 17
  72. set_grey(float f)
  73. {
  74.     int i,j,m;
  75.     i = (1.0-f)*(NGREY-.8);
  76.     m = grey_bits[i];
  77.     for (j=0; j<4; j++) {
  78.         cur_bits[j] = m & 0x000f;
  79.         m = m >> 4;
  80.     }
  81. }
  82. bitmap_paint(int row, int x1, int x2)
  83. {
  84.     int i,xm,ym,xadd=0;
  85.     unsigned char *line;
  86.     x1-=1; x2-=1;
  87.     if (x1<0) x1 = 0;
  88.     if (x2>=nxbits) x2 = nxbits-1;
  89.     ym = row % 4;
  90.     if ((row & 4)>0) xadd = 1;
  91.     line = bitmap_line(row);
  92.     for (;x1<=x2;x1++) {
  93.         xm = 1 << ((x1+xadd) % 4);
  94.         if ((cur_bits[ym] & xm)!=0) {
  95.             i = line[x1/2];
  96.             if (x1 % 2==0) i = (i & 0xf0) | (cur_filli << 4);
  97.             else i = (i & 0x0f) | cur_filli;
  98.             line[x1/2] = i;
  99.         }
  100.     }
  101. }
  102. unsigned char *bitmapv[NYBITS];
  103. int bitmapn[NYBITS];
  104. char *bitmap_line(int y)
  105. {
  106.     static unsigned char bitrow[NXBITS/2+3];
  107.     static unsigned char bitc[NXBITS],*o,*bc,c;
  108.     int nc,x,i,j;
  109.     static lasty = -2;
  110.     if (lasty==y) return bitrow;
  111.     if (y<0 || y>=nybits) printf("y is out of range %d \n",y);
  112.     if (y>=nybits) y = nybits-1;
  113.     dp printf("--Core left %ld  line %d \n",coreleft(),y);
  114.  
  115.     if (lasty != -2) {
  116.       o = &bitc[0];
  117.       for (x=0; x<(nxbits/2) ; x++) {
  118.         c = bitrow[x];
  119.         nc = 0;
  120.         for (;x<(nxbits/2-1) && bitrow[x+1]==c && nc<250; x++) nc++;
  121.         *o++ = nc;
  122.         *o++ = c;
  123.       }
  124.       nc = o-bitc;
  125.       bitmapn[lasty] = nc;
  126.       if (bitmapv[lasty]!=NULL) bitmapv[lasty] = realloc(bitmapv[lasty],nc);
  127.       else bitmapv[lasty] = malloc(nc);
  128.       if (bitmapv[lasty]==0) {
  129.         printf("Failed to allocate enough memory for bitmap \n");
  130.         abort();
  131.       }
  132.       dp printf("saving line length %d \n",nc);
  133.       memcpy(bitmapv[lasty],bitc,nc);
  134.       o = bitmapv[lasty];
  135.     }
  136.     lasty = y;
  137.     if (bitmapv[y]==NULL) {
  138.         memset(bitrow,0,nxbits/2);
  139.  dp        printf("returning blank line\n");
  140.         return bitrow;
  141.     }
  142.     dp printf("Expanding line %d (%p) \n",y,bitmapv[y]);
  143.  
  144.     nc = 0;
  145.     bc = bitmapv[y];
  146.     for (i=0, o = bitrow; i<bitmapn[y]; i+=2) {
  147.         dp printf("E, %d %d \n",bc[i],bc[i+1]);
  148.         for (j=0; j<=bc[i]; j++) {
  149.             *o++ = bc[i+1]; nc++;
  150.             if (nc>nxbits/2) {
  151.                 printf("gone past end\n");
  152.                 return bitrow;
  153.             }
  154.         }
  155.     }
  156.     return bitrow;
  157. }
  158. bitmap_pixel(int x, int y)
  159. {
  160.     static char *line;
  161.     static int ly= -2,i;
  162. /*    if (x<0 || x>nxbits || y < 0 || y > nybits) return; */
  163.     line = bitmap_line(y);
  164.     i = line[x/2];
  165.     if (x % 2==0) i = (i & 0xf0) | (cur_colori << 4);
  166.     else i = (i & 0x0f) | cur_colori;
  167.     line[x/2] = i;
  168. /*     line[x/2] |= (1 << (x % 2)); */
  169.     ly = y;
  170. }
  171.  
  172. dvitype(void)
  173. {
  174.     printf("Usage: DVIPJ   [-old]   [-low]   [outfile.prt]\n");
  175.     if (dev_low) printf("Using 90 DPI\n");
  176.     if (hp_plus)
  177.      printf("HP PaintJet, (-old turns of data compression)");
  178.     else
  179.      printf("HP PaintJet, (COMPRESSION OFF)");
  180. }
  181. int ljsendline(char *s, int nc, int y, int plane);
  182.  
  183. /*
  184. #ifdef __TURBOC__
  185. #include <graphics.h>
  186. xxbitmap_print()
  187. {
  188.     int x,y,z;
  189.     int d_graphmode=0,g_driver=0,g_error;
  190.     double f;
  191.     unsigned char *line;
  192.     printf("press return to enter graphics \n");
  193.     getch();
  194.     dop = false;
  195.     detectgraph(&g_driver, &d_graphmode);
  196.     if (g_driver<0) {
  197.         printf("No graphics hardware detected !!!!! \n");
  198.         gle_abort("Could not load BGI graphics\n");
  199.     }
  200.     initgraph(&g_driver,&d_graphmode,"");
  201.     g_error = graphresult();
  202.     if (g_error<0) {
  203.         printf("Init graph error %s\n",grapherrormsg(g_error));
  204.         gle_abort("Init graph error\n");
  205.     }
  206.  
  207.     for (y=1; y<399; y++) {
  208.       line = bitmap_line(y);
  209.       for (x=0; x<620 ; x++) {
  210.         z = 1 << (x % 8);
  211.         if ((z & line[x/8]) != 0) {
  212.              putpixel(x,y,2);
  213.         }
  214.  
  215.       }
  216.     }
  217.  
  218.     getch();
  219.  
  220.     closegraph();
  221. }
  222. #endif
  223. */
  224. int bitplane(char line[],int x, int p);
  225.  
  226. bitmap_print(void)
  227. {
  228.     int x,y;
  229.     char *line;
  230.     static unsigned char out_buff[NXBITS+20];
  231.     unsigned char swapbit[256];
  232.     unsigned char swapnib[16]={0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
  233.     unsigned char *o,c;
  234.     int nc,i,n1,n2,plane;
  235.  
  236.     printf("Writing out bitmap\n");
  237.     for (i=0;i<256;i++) {
  238.         n1 = i & 0xf;
  239.         n2 = (i >> 4) & 0xf;
  240.         swapbit[i] = swapnib[n2] | (swapnib[n1] << 4);
  241.     }
  242.     pprintf("%cE",27);     /* reset  */
  243.     if (dev_low)  pprintf("\x1b*t90R");    /* set resoulution 150 dpi */
  244.     else          pprintf("\x1b*t180R");    /* set resoulution 150 dpi */
  245.         /*pprintf("\x1b&a%.1fH",0.0);          move x */
  246.         /*pprintf("\x1b&a%.1fV",((NYBITS-y)/300.0)*720.0);     move y */
  247.     if (hp_plus) pprintf("\x1b*b1M");   /* compact,  not used as I think it doesn't work */
  248.     pprintf("\x1b*r3U");    /* use 3 color planes, 8 colors*/
  249.     pprintf("\x1b*r0A");    /* start graphics at left margin */
  250.     for (y=(nybits-1); y>=0; y--) {
  251.       line = bitmap_line(y);
  252.       for (plane = 0; plane < 3; plane++) {
  253.         o = &out_buff[0];
  254.         for (x=0; x<nxbits/8; x++) {
  255.         c = bitplane(line,x,plane);
  256.         nc = 0;
  257.         if (hp_plus) {
  258.             for (;x<(nxbits/8-1) && bitplane(line,x+1,plane)==c
  259.                 && nc<250; x++) nc++;
  260.             *o++ = nc;
  261.         }
  262.         *o++ = swapbit[c];
  263.         }
  264.         ljsendline(out_buff,o-out_buff,y,plane);
  265.       }
  266.     }
  267.     pprintf("\x1b*rB");        /* end grpahics */
  268.     pprintf("\x0c"); /* form feed, reset origin */
  269. }
  270. bitplane(char line[],int x, int p)
  271. {
  272.     /* x is byte count */
  273.     /* construct a byte from line[x].hi.low.p  ... line[x+7] */
  274.     register xx,i,curbit,c,pm;
  275.     c = 0;
  276.     x = x*8;
  277.     pm = 1 << p;
  278.     curbit = 1;
  279.     for (xx=x; xx<x+8; xx++) {
  280.         i = line[xx/2];
  281.         if (xx % 2==0) i = (i & 0xf0)>>4;
  282.         else i = i & 0xf;
  283.         i = i & pm;
  284.         if (i==0) c = c + curbit;
  285.         curbit = curbit << 1;
  286.     }
  287.     return c;
  288. }
  289. ljsendline(char *s, int nc,int y,int plane)
  290. {
  291.     if (plane==2)     pprintf("\x1b*b%dW",nc);    /* send y bytes */
  292.     else         pprintf("\x1b*b%dV",nc);    /* send y bytes */
  293.  
  294.     printmem(s,nc);
  295. }
  296.  
  297.  
  298.  
  299.